/* 
 * File:   FileCachedCleanServer.cpp
 * Author: dell
 * 
 * Created on 2012年11月21日, 下午4:26
 */



#include "FileCachedCleanServer.h"
#include "FileCachedCleanTimer.h"
#include "FileCachedCleanProcessQuitHandler.h"
#include "ServerPid.h"
#include <evhttp/HttpServer.h>
#include "RapidServer.h"
#include <sys/stat.h>
#include <time.h>
//#include <linux/inotify.h>
#include <sys/inotify.h>
#include <event.h>  
#include <linux/unistd.h>
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  


using namespace std;

namespace Rapid{
    FileCachedCleanServer::FileCachedCleanServer(RapidConfigManager* rapidConfigManager, string& hdir):
        Process(false, false),rapidConfigManager(rapidConfigManager),homeDir(hdir) {
    }

    FileCachedCleanServer::~FileCachedCleanServer() {
        processMessage.close();
    }

    bool FileCachedCleanServer::start(void* param){
        return Process::start(param);
    }
    
    int FileCachedCleanServer::run(void* param){
       
        ev::loop_ref loop = ev::get_default_loop();
        loop.post_fork();        
        
        //将pid写入server.pid文件
        ServerPid serverPid(homeDir.c_str(), 0);
        serverPid.append(getpid());        
        
        //启动缓存文件同步清理定时器
        FileCachedCleanTimer timer(rapidConfigManager);
        timer.set(1.0, 10.0); //下一步可配置,
        timer.start();
        
        //启动进程退出时事件处理程序
        FileCachedCleanProcessQuitHandler quithander(homeDir);
        quithander.start();
        
        loop.run(0);

        return 0;
    }    
    
    
    bool FileCachedCleanServer::onTimerEvent(const TimerEvent* e){
        #define fnamebufsize 1024
        char filename[fnamebufsize] = {0};
        ssize_t rsize = processMessage.receive(filename, fnamebufsize);
        if(rsize > 0){
            LOG_DEBUG("filename:%s", filename);
            
            
        }
        
    }
    
    void FileCachedCleanServer::notifyCallback(ev::io& io, int revents){


    }
    
}


